const express = require('express')
const postRouter = express.Router()
const {
  gettoken,
  verifyToken,
  date_fns,
  json,
  cors,
  multer,
  upload1,
  upload2,
  upload3,
  lunbo,
  db,
} = require('../tools')
//密码加密
const bcrypt = require('bcryptjs')
var salt = bcrypt.genSaltSync(10)

/**
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */

// 用户登录验证
postRouter.post('/login', (req, res) => {
  let { username, password } = req.body
  const sql = `SELECT * FROM users WHERE username = ?`
  db.query(sql, [username], (err, data) => {
    if (err) {
      console.log(err)
      return res.status(500).send('服务器错误')
    }
    if (data[0]) {
      const token = gettoken(username, '7d')
      if (bcrypt.compareSync(password, data[0].password)) {
        console.log('登录接口访问成功', date_fns())
        console.log('-------------------------------------')
        res.send([token, '登录成功', data])
      } else {
        res.send('密码错误,由6~12位数字和字母(大写或小写)混合组成)')
      }
    } else {
      res.send('该用户不存在，请先注册')
    }
  })
})

// 注册
postRouter.post('/register', (req, res) => {
  // console.log(req.body)
  let { username, password, phone, gender } = req.body
  let hash = bcrypt.hashSync(password, salt)
  const sql = `select * from users where username='${username}'`
  db.query(sql, (err, data) => {
    if (err) return console.log('注册接口错误', err.message)
    if (data[0]) {
      res.send('用户已存在')
    } else {
      const sql = `insert into users (username,password,phone,gender) values ('${username}','${hash}','${phone}','${gender}')`
      db.query(sql, (err, data) => {
        if (err) return console.log('注册接口错误', err.message)
        // console.log(data)
      })
      res.send('注册成功，请重新登录')
      console.log('注册接口访问成功', date_fns())
      console.log('-------------------------------------')
    }
  })
})
// 修改个人信息
postRouter.post('/xiuxi', (req, res) => {
  let { username, gender, phone } = req.body
  const sql = `update users set gender='${gender}',phone='${phone}' where username='${username}'`
  db.query(sql, (err, data) => {
    if (err) return console.log('接口错误', err.message)
    res.send('修改成功')
  })
})
// 修改密码
postRouter.post('/updatepwd', (req, res) => {
  let { username, password, phone } = req.body
  const sql = `select * from users where username='${username}'`
  db.query(sql, (err, data) => {
    if (err) return console.log('修改密码接口错误', err.message)
    if (data[0]) {
      const sql1 = `select phone from users where username='${username}'`
      db.query(sql, (err, data) => {
        if (err) {
          return console.log('错误', err.message)
        } else {
          if (data[0].phone == phone) {
            let hash = bcrypt.hashSync(password, salt)
            const sql = `update users set password='${hash}' where username='${username}'`
            db.query(sql, (err, data) => {
              if (err) return console.log('修改密码接口错误', err.message)
            })
            res.send('修改成功，请重新登录')
            console.log('修改密码接口访问成功', date_fns())
            console.log('-------------------------------------')
          } else {
            res.send('手机号错误')
          }
        }
      })
    } else {
      res.send('用户不存在')
    }
  })
})

// 度假--测试接口
postRouter.post('/dujia', verifyToken, (req, res) => {
  // let { user, pwd } = req.body
  // console.log(user, pwd)
  const sql = `select * from users`
  db.query(sql, (err, data) => {
    if (err) {
      return console.log('错误', err.message)
    } else {
      res.send(data)
    }
  })
})

// 用户头像上传接口
postRouter.post('/upload', upload1.array('files', 1), (req, res) => {
  //upload.single()里边的file是指定上传formdata时的键名
  let username = req.query.username
  if (req.files.length > 0) {
    let [size, mimetype, filename] = [0, [], []]
    req.files.forEach((i) => {
      size += i.size // 文件大小
      mimetype.push(i.mimetype.split('/')[1]) // 文件类型
      filename.push(i.filename) // 文件名称
    })
    let types = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg'] //限制上传的文件类型

    let Typeflag = true
    for (let i = 0; i < mimetype.length; i++) {
      if (types.includes(mimetype[i])) {
        Typeflag = true
      } else {
        Typeflag = false
        break
      }
    }
    // 限制尺寸(不大于10m)
    if (size >= 10 * 1024 * 1024) {
      return res.json({ code: 1, msg: '文件过大' })
    } else if (!Typeflag) {
      return res.json({ code: 1, msg: '类型错误' })
    } else {
      res.json({ status: 200, msg: filename.join(';') })
      console.log('头像上传成功')
      const sql = `select * from users where username='${username}'`
      db.query(sql, (err, data) => {
        if (err) return console.log('头像接口错误', err.message)
        if (data[0]) {
          let head_img = filename[0]
          const sql = `update users set head_img='http://localhost:3000/touxiang/${head_img}' where username='${username}'`
          db.query(sql, (err, data) => {
            if (err) return console.log('修改接口错误', err.message)
            // console.log(data)
          })
          console.log('头像接口访问成功', date_fns())
          console.log('-------------------------------------')
        } else {
          res.send('用户不存在')
        }
      })
    }
  } else {
    res.json({ code: 1, msg: '上传文件不能为空' })
  }
})

// 领养信息上传接口
postRouter.post('/lyxinxi', (req, res) => {
  // console.log(req.body)
  let { username, true_name, phone, pet_id, adopt_time, address } = req.body
  const sql = `insert into adopts (username, true_name, phone, pet_id, adopt_time, address,status) values ('${username}','${true_name}','${phone}','${pet_id}','${adopt_time}','${address}','待审核')`
  db.query(sql, (err, data) => {
    if (err) return console.log('领养信息上传接口错误', err.message)
    const sql = `update pets set status='待审核' where pet_id='${pet_id}'`
    db.query(sql, (err, data) => {
      if (err) {
        return console.log('修改宠物状态错误', err.message)
      } else {
        res.send('上传成功')
        console.log('领养信息上传接口访问成功', date_fns())
        console.log('-------------------------------------')
      }
    })
  })
})

// 救助信息上传接口
postRouter.post('/jiuzhu', (req, res) => {
  // console.log(req.body)
  let { true_name, username, phone, address, remark, breed } = req.body
  const sql = `insert into helps (true_name,username, phone, address,remark,breed,go_time,status ) values ('${true_name}','${username}','${phone}','${address}','${remark}','${breed}','${date_fns()}','等待救助中')`
  db.query(sql, (err, data) => {
    if (err) return console.log('救助信息上传接口错误', err.message)
    res.send(data)
    console.log('救助信息上传接口访问成功', date_fns())
    console.log('-------------------------------------')
  })
})

// 发布救助照片上传接口
postRouter.post('/jiuimg', upload3.array('files', 1), (req, res) => {
  //upload.single()里边的file是指定上传formdata时的键名
  let help_id = req.query.user

  if (req.files.length > 0) {
    let [size, mimetype, filename] = [0, [], []]
    req.files.forEach((i) => {
      size += i.size // 文件大小
      mimetype.push(i.mimetype.split('/')[1]) // 文件类型
      filename.push(i.filename) // 文件名称
    })
    let types = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg'] //限制上传的文件类型

    let Typeflag = true
    for (let i = 0; i < mimetype.length; i++) {
      if (types.includes(mimetype[i])) {
        Typeflag = true
      } else {
        Typeflag = false
        break
      }
    }
    // 限制尺寸(不大于10m)
    if (size >= 10 * 1024 * 1024) {
      return res.json({ code: 1, msg: '文件过大' })
    } else if (!Typeflag) {
      return res.json({ code: 1, msg: '类型错误' })
    } else {
      res.json({ status: 200, msg: filename.join(';') })
      // console.log('救助照片上传成功')
      const sql = `select * from helps where help_id='${help_id}'`
      db.query(sql, (err, data) => {
        if (err) return console.log('救助图片接口错误', err.message)
        if (data[0]) {
          let help_img = filename[0]
          const sql = `update helps set befor_img='http://localhost:3000/helps/${help_img}' where help_id='${help_id}'`
          db.query(sql, (err, data) => {
            if (err) return console.log('救助照片上传接口错误', err.message)
            // console.log(data)
          })
          console.log('发起救助图片接口访问成功', date_fns())
          console.log('-------------------------------------')
        } else {
          res.send('数据不存在')
        }
      })
    }
  } else {
    // res.json({ code: 1, msg: '上传文件不能为空' })
  }
})

//论坛发表接口
postRouter.post('/luntan', (req, res) => {
  // console.log(req.body)
  let { username, title, content } = req.body
  const sql = `insert into forums (username,title, content ,publish_time ) values ('${username}','${title}','${content}','${date_fns()}')`
  db.query(sql, (err, data) => {
    if (err) return console.log('论坛发表接口错误', err.message)
    res.send(data)
    console.log('论坛发表接口访问成功', date_fns())
    console.log('-------------------------------------')
  })
})
//论坛删除接口
postRouter.post('/ltshan', (req, res) => {
  let { id } = req.body
  const sql = `delete from forums where forum_id=${id}`
  db.query(sql, (err, data) => {
    if (err) return console.log('论坛删除接口错误', err.message)
    const sql = `delete from reviews where forum_id=${id}`
    db.query(sql, (err, data) => {
      if (err) return console.log('论坛删除接口错误', err.message)
      res.send('删除成功')
      console.log('论坛删除接口访问成功', date_fns())
      console.log('-------------------------------------')
    })
  })
})
//评论发表接口
postRouter.post('/pinglun', (req, res) => {
  // console.log(req.body)
  let { username, forum_id, content } = req.body
  const sql = `insert into reviews (username,forum_id, review ,review_time) values ('${username}','${forum_id}','${content}','${date_fns()}')`
  db.query(sql, (err, data) => {
    if (err) return console.log('评论发表接口错误', err.message)
    res.send(data)
    console.log('评论发表接口访问成功', date_fns())
    console.log('-------------------------------------')
  })
})
//评论删除接口
postRouter.post('/plshan', (req, res) => {
  let { id } = req.body
  const sql = `delete from reviews where review_id=${id}`
  db.query(sql, (err, data) => {
    if (err) return console.log('评论删除接口错误', err.message)
    res.send('删除成功')
    console.log('评论删除接口访问成功', date_fns())
    console.log('-------------------------------------')
  })
})

/**
 *
 * 管理员
 *
 * 这里往下是
 * 后台系统
 *
 */

// 管理员登录验证
postRouter.post('/glylogin', (req, res) => {
  let { username, password } = req.body
  const sql = `SELECT * FROM manager WHERE username = ?`
  db.query(sql, [username], (err, data) => {
    if (err) {
      console.log(err)
      return res.status(500).send('服务器错误')
    }
    if (data[0]) {
      if (password == data[0].password) {
        console.log('管理员登录接口访问成功', date_fns())
        console.log('-------------------------------------')
        res.send(['登录成功', data])
      } else {
        res.send('密码错误,由6~12位数字和字母(大写或小写)混合组成')
      }
    } else {
      res.send('账号错误')
    }
  })
})

// 重置用户密码
postRouter.post('/czpass', (req, res) => {
  let { username } = req.body
  let hash = bcrypt.hashSync('abc123', salt)
  const sql = `update users set password='${hash}' where username='${username}'`
  db.query(sql, (err, data) => {
    if (err) return console.log('重置用户密码接口错误', err.message)
    res.send('重置密码成功！')
    console.log('重置用户密码接口访问成功', date_fns())
    console.log('-------------------------------------')
  })
})
// 重置用户头像
postRouter.post('/czimg', (req, res) => {
  let { username } = req.body
  const sql = `update users set head_img='http://localhost:3000/touxiang/moren.jpg' where username='${username}'`
  db.query(sql, (err, data) => {
    if (err) return console.log('重置用户头像接口错误', err.message)
    res.send('重置头像成功！')
    console.log('重置用户头像接口访问成功', date_fns())
    console.log('-------------------------------------')
  })
})

//用户删除接口
postRouter.post('/usershan', (req, res) => {
  let { username } = req.body
  const sql = `delete from users where username='${username}'`
  db.query(sql, (err, data) => {
    if (err) return console.log('用户删除接口错误', err.message)
    res.send('用户删除成功！')
    console.log('用户删除接口访问成功', date_fns())
    console.log('-------------------------------------')
  })
})

// 修改轮播图接口
postRouter.post('/xiulun', lunbo.array('files', 1), (req, res) => {
  let lunboid = req.query.lunboid
  //upload.single()里边的file是指定上传formdata时的键名
  if (req.files.length > 0) {
    let [size, mimetype, filename] = [0, [], []]
    req.files.forEach((i) => {
      size += i.size // 文件大小
      mimetype.push(i.mimetype.split('/')[1]) // 文件类型
      filename.push(i.filename) // 文件名称
    })
    let types = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg'] //限制上传的文件类型
    let Typeflag = true
    for (let i = 0; i < mimetype.length; i++) {
      if (types.includes(mimetype[i])) {
        Typeflag = true
      } else {
        Typeflag = false
        break
      }
    }
    // 限制尺寸(不大于10m)
    if (size >= 10 * 1024 * 1024) {
      return res.json({ code: 1, msg: '文件过大' })
    } else if (!Typeflag) {
      return res.json({ code: 1, msg: '类型错误' })
    } else {
      res.json({ status: 200, msg: filename.join(';') })
      //这里存数据库
      let img = filename[0]
      if (lunboid) {
        const sql = `update lunbo set lunbo='http://localhost:3000/lunbo/${img}' where lunbo_id='${lunboid}'`
        db.query(sql, (err, data) => {
          if (err) return console.log('修改轮播图接口错误', err.message)
          console.log('修改轮播图接口访问成功', date_fns())
          console.log('-------------------------------------')
        })
      }
    }
  } else {
    res.json({ code: 1, msg: '上传文件不能为空' })
  }
})
// 添加轮播图接口
postRouter.post('/tianlun', lunbo.array('file', 1), (req, res) => {
  //upload.single()里边的file是指定上传formdata时的键名
  if (req.files.length > 0) {
    let [size, mimetype, filename] = [0, [], []]
    req.files.forEach((i) => {
      size += i.size // 文件大小
      mimetype.push(i.mimetype.split('/')[1]) // 文件类型
      filename.push(i.filename) // 文件名称
    })
    let types = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg'] //限制上传的文件类型

    let Typeflag = true
    for (let i = 0; i < mimetype.length; i++) {
      if (types.includes(mimetype[i])) {
        Typeflag = true
      } else {
        Typeflag = false
        break
      }
    }
    // 限制尺寸(不大于10m)
    if (size >= 10 * 1024 * 1024) {
      return res.json({ code: 1, msg: '文件过大' })
    } else if (!Typeflag) {
      return res.json({ code: 1, msg: '类型错误' })
    } else {
      res.json({ status: 200, msg: filename.join(';') })
      //这里存数据库
      let img = filename[0]
      const sql = `insert into lunbo (lunbo) values ('http://localhost:3000/lunbo/${img}')`
      db.query(sql, (err, data) => {
        if (err) return console.log('轮播图增加接口错误', err.message)
        console.log('轮播图增加接口访问成功', date_fns())
        console.log('-------------------------------------')
      })
    }
  } else {
    res.json({ code: 1, msg: '上传文件不能为空' })
  }
})
//轮播图删除接口
postRouter.post('/lunshan', (req, res) => {
  let { lunboid } = req.body
  const sql = `delete from lunbo where lunbo_id='${lunboid}'`
  db.query(sql, (err, data) => {
    if (err) return console.log('轮播图删除接口错误', err.message)
    res.send('轮播图删除成功！')
    console.log('轮播图删除接口访问成功', date_fns())
    console.log('-------------------------------------')
  })
})

//每日资讯修改接口
postRouter.post('/xiuzixun', (req, res) => {
  let { title, content } = req.body
  const sql = `update zixun set title='${title}',content='${content}'`
  db.query(sql, (err, data) => {
    if (err) return console.log('每日资讯修改接口错误', err.message)
    res.send('每日资讯修改成功！')
    console.log('每日资讯修改接口访问成功', date_fns())
    console.log('-------------------------------------')
  })
})

// 领养图片上传接口
postRouter.post('/adoptimg', upload2.array('files', 1), (req, res) => {
  // console.log(req.query.pet_id)
  let pet_id = req.query.pet_id
  let zuo = req.query.zuo
  console.log(zuo)

  //upload.single()里边的file是指定上传formdata时的键名
  if (req.files.length > 0) {
    let [size, mimetype, filename] = [0, [], []]
    req.files.forEach((i) => {
      size += i.size // 文件大小
      mimetype.push(i.mimetype.split('/')[1]) // 文件类型
      filename.push(i.filename) // 文件名称
    })
    let types = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg'] //限制上传的文件类型

    let Typeflag = true
    for (let i = 0; i < mimetype.length; i++) {
      if (types.includes(mimetype[i])) {
        Typeflag = true
      } else {
        Typeflag = false
        break
      }
    }
    // 限制尺寸(不大于10m)
    if (size >= 10 * 1024 * 1024) {
      return res.json({ code: 1, msg: '文件过大' })
    } else if (!Typeflag) {
      return res.json({ code: 1, msg: '类型错误' })
    } else {
      res.json({ status: 200, msg: filename.join(';') })
      //这里存数据库
      let img = filename[0]
      if (zuo == 1) {
        const sql = `update pets set image='http://localhost:3000/lingyang/${img}' where pet_id='${pet_id}'`
        db.query(sql, (err, data) => {
          if (err) return console.log('领养图片上传接口错误', err.message)
          console.log('领养图片上传接口访问成功', date_fns())
          console.log('-------------------------------------')
        })
      }
    }
  } else {
    res.json({ code: 1, msg: '上传文件不能为空' })
  }
})

//宠物信息修改接口
postRouter.post('/xiuadopt', (req, res) => {
  let { pet_id, datas, ziduan } = req.body

  if (ziduan == 1) {
    const sql = `update pets set petname='${datas}' where pet_id='${pet_id}'`
    db.query(sql, (err, data) => {
      if (err) return console.log('宠物名字修改接口错误', err.message)
      res.send('宠物名字修改成功！')
      console.log('宠物名字修改接口访问成功', date_fns())
      console.log('-------------------------------------')
    })
  }
  if (ziduan == 2) {
    const sql = `update pets set breed='${datas}' where pet_id='${pet_id}'`
    db.query(sql, (err, data) => {
      if (err) return console.log('宠物类型修改接口错误', err.message)
      res.send('宠物类型修改成功！')
      console.log('宠物类型修改接口访问成功', date_fns())
      console.log('-------------------------------------')
    })
  }
  if (ziduan == 3) {
    const sql = `update pets set gender='${datas}' where pet_id='${pet_id}'`
    db.query(sql, (err, data) => {
      if (err) return console.log('宠物性别修改接口错误', err.message)
      res.send('宠物性别修改成功！')
      console.log('宠物性别修改接口访问成功', date_fns())
      console.log('-------------------------------------')
    })
  }
  if (ziduan == 4) {
    const sql = `update pets set sterilization='${datas}' where pet_id='${pet_id}'`
    db.query(sql, (err, data) => {
      if (err) return console.log('宠物是否绝育修改接口错误', err.message)
      res.send('宠物是否绝育修改成功！')
      console.log('宠物是否绝育修改接口访问成功', date_fns())
      console.log('-------------------------------------')
    })
  }
  if (ziduan == 5) {
    const sql = `update pets set health='${datas}' where pet_id='${pet_id}'`
    db.query(sql, (err, data) => {
      if (err) return console.log('宠物是否打疫苗修改接口错误', err.message)
      res.send('宠物是否打疫苗修改成功！')
      console.log('宠物是否打疫苗修改接口访问成功', date_fns())
      console.log('-------------------------------------')
    })
  }
  if (ziduan == 6) {
    const sql = `update pets set region='${datas}' where pet_id='${pet_id}'`
    db.query(sql, (err, data) => {
      if (err) return console.log('宠物区域修改接口错误', err.message)
      res.send('宠物区域修改成功！')
      console.log('宠物区域修改接口访问成功', date_fns())
      console.log('-------------------------------------')
    })
  }
  if (ziduan == 7) {
    const sql = `update pets set briefing='${datas}' where pet_id='${pet_id}'`
    db.query(sql, (err, data) => {
      if (err) return console.log('宠物描述修改接口错误', err.message)
      res.send('宠物描述修改成功！')
      console.log('宠物描述修改接口访问成功', date_fns())
      console.log('-------------------------------------')
    })
  }
  if (ziduan == 8) {
    const sql = `update pets set age='${datas}' where pet_id='${pet_id}'`
    db.query(sql, (err, data) => {
      if (err) return console.log('宠物年龄修改接口错误', err.message)
      res.send('宠物年龄修改成功！')
      console.log('宠物年龄修改接口访问成功', date_fns())
      console.log('-------------------------------------')
    })
  }
})
//添加宠物接口
postRouter.post('/tianadopt', (req, res) => {
  let { petname, gender, age, breed, sterilization, health, region, briefing } =
    req.body
  const sql = `insert into pets (petname,gender,age,breed,sterilization,health,region,briefing,publish_time,status) values ('${petname}','${gender}','${age}','${breed}','${sterilization}','${health}','${region}','${briefing}','${
    date_fns().split(' ')[0]
  }','待领养')`
  db.query(sql, (err, data) => {
    if (err) return console.log('添加宠物接口错误', err.message)
    res.send(['添加成功！', data])
    console.log('论添加宠物接口访问成功', date_fns())
    console.log('-------------------------------------')
  })
})
//宠物删除接口
postRouter.post('/petshan', (req, res) => {
  let { pet_id } = req.body
  const sql = `delete from pets where pet_id='${pet_id}'`
  db.query(sql, (err, data) => {
    if (err) return console.log('宠物删除接口错误', err.message)
    res.send('宠物删除成功！')
    console.log('宠物删除接口访问成功', date_fns())
    console.log('-------------------------------------')
  })
})
// 审核通过
postRouter.post('/xiushen', (req, res) => {
  let { pet_id, adopt_id } = req.body
  const sql = `update adopts set status='审核通过，快去领养吧！',adopt_status='待领养' where adopt_id='${adopt_id}'`
  db.query(sql, (err, data) => {
    if (err) return console.log('审核通过接口错误', err.message)
    const sql = `update pets set status='审核通过，待领养' where pet_id='${pet_id}'`
    db.query(sql, (err, data) => {
      if (err) return console.log('修改审核状态接口错误', err.message)
      res.send('修改成功！')
      console.log('审核通过接口访问成功', date_fns())
      console.log('-------------------------------------')
    })
  })
})
// 审核未通过
postRouter.post('/tongshen', (req, res) => {
  let { pet_id, content, adopt_id } = req.body
  const sql = `update adopts set status='审核未通过',reason='${content}',adopt_status='领养失败' where adopt_id='${adopt_id}'`
  db.query(sql, (err, data) => {
    if (err) return console.log('修改审核状态接口错误', err.message)
    const sql = `update pets set status='待领养' where pet_id='${pet_id}'`
    db.query(sql, (err, data) => {
      if (err) return console.log('修改审核状态接口错误', err.message)
      res.send('修改成功！')
      console.log('修改审核状态接口访问成功', date_fns())
      console.log('-------------------------------------')
    })
  })
})
// 确定领养
postRouter.post('/queling', (req, res) => {
  let { pet_id, adopt_id } = req.body
  const sql = `update adopts set adopt_status='领养成功' where adopt_id='${adopt_id}'`
  db.query(sql, (err, data) => {
    if (err) return console.log('确定领养接口错误', err.message)
    const sql = `update pets set status='已领养' where pet_id='${pet_id}'`
    db.query(sql, (err, data) => {
      if (err) return console.log('确定领养接口错误', err.message)
      res.send('修改成功！')
      console.log('确定领养访问成功', date_fns())
      console.log('-------------------------------------')
    })
  })
})
//宠物已领养记录删除接口
postRouter.post('/yilingshan', (req, res) => {
  let { pet_id, adopt_id } = req.body
  const sql = `delete from pets where pet_id='${pet_id}'`
  db.query(sql, (err, data) => {
    if (err) return console.log('宠物已领养记录删除接口错误', err.message)
    const sql = `delete from adopts where adopt_id='${adopt_id}'`
    db.query(sql, (err, data) => {
      if (err) return console.log('宠物已领养记录删除接口错误', err.message)
      res.send('宠物删除成功！')
      console.log('宠物已领养记录删除接口访问成功', date_fns())
      console.log('-------------------------------------')
    })
  })
})
// 前往救助
postRouter.post('/jiushen', (req, res) => {
  let { help_id, state, content } = req.body
  if (state == 1) {
    const sql = `update helps set status='救助中' where help_id='${help_id}'`
    db.query(sql, (err, data) => {
      if (err) return console.log('救助审核通过接口错误', err.message)
      res.send('修改成功！')
      console.log('救助审核通过接口访问成功', date_fns())
      console.log('-------------------------------------')
    })
  }
  if (state == 2) {
    const sql = `update helps set status='救助失败',reason='${content}' where help_id='${help_id}'`
    db.query(sql, (err, data) => {
      if (err) return console.log('救助审核未通过接口错误', err.message)
      res.send('修改成功！')
      console.log('救助审核未通过接口访问成功', date_fns())
      console.log('-------------------------------------')
    })
  }
  if (state == 3) {
    const sql = `update helps set status='救助完成',end_time='${date_fns()}' where help_id='${help_id}'`
    db.query(sql, (err, data) => {
      if (err) return console.log('救助完成接口错误', err.message)
      res.send('修改成功！')
      console.log('救助完成接口访问成功', date_fns())
      console.log('-------------------------------------')
    })
  }
  if (state == 4) {
    const sql = `delete from helps where help_id='${help_id}'`
    db.query(sql, (err, data) => {
      if (err) return console.log('救助信息删除接口错误', err.message)
      res.send('修改成功！')
      console.log('救助信息删除接口访问成功', date_fns())
      console.log('-------------------------------------')
    })
  }
})
// 救助完成照片上传接口
postRouter.post('/jiuwanimg', upload3.array('files', 1), (req, res) => {
  //upload.single()里边的file是指定上传formdata时的键名
  let help_id = req.query.help_id

  if (req.files.length > 0) {
    let [size, mimetype, filename] = [0, [], []]
    req.files.forEach((i) => {
      size += i.size // 文件大小
      mimetype.push(i.mimetype.split('/')[1]) // 文件类型
      filename.push(i.filename) // 文件名称
    })
    let types = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg'] //限制上传的文件类型

    let Typeflag = true
    for (let i = 0; i < mimetype.length; i++) {
      if (types.includes(mimetype[i])) {
        Typeflag = true
      } else {
        Typeflag = false
        break
      }
    }
    // 限制尺寸(不大于10m)
    if (size >= 10 * 1024 * 1024) {
      return res.json({ code: 1, msg: '文件过大' })
    } else if (!Typeflag) {
      return res.json({ code: 1, msg: '类型错误' })
    } else {
      res.json({ status: 200, msg: filename.join(';') })
      // console.log('救助照片上传成功')
      let help_img = filename[0]
      const sql = `update helps set after_img='http://localhost:3000/helps/${help_img}' where help_id='${help_id}'`
      db.query(sql, (err, data) => {
        if (err) return console.log('救助照片上传接口错误', err.message)
        // console.log(data)
      })
      console.log('发起救助图片接口访问成功', date_fns())
      console.log('-------------------------------------')
    }
  } else {
    res.json({ code: 1, msg: '上传文件不能为空' })
  }
})
//小知识添加接口
postRouter.post('/jiazhishi', (req, res) => {
  // console.log(req.body)
  let { title, content } = req.body
  const sql = `insert into knowledge (title, content ,knowledge_time ) values ('${title}','${content}','${
    date_fns().split(' ')[0]
  }')`
  db.query(sql, (err, data) => {
    if (err) return console.log('小知识添加接口错误', err.message)
    res.send('添加成功')
    console.log('小知识添加接口访问成功', date_fns())
    console.log('-------------------------------------')
  })
})
//小知识修改接口
postRouter.post('/xiuzhi', (req, res) => {
  // console.log(req.body)
  let { knowledge_id, title, content } = req.body
  const sql = `update knowledge set title='${title}', content='${content}' where knowledge_id=${knowledge_id}`
  db.query(sql, (err, data) => {
    if (err) return console.log('小知识修改接口错误', err.message)
    res.send('修改成功')
    console.log('小知识修改接口访问成功', date_fns())
    console.log('-------------------------------------')
  })
})
//小知识删除接口
postRouter.post('/zhishan', (req, res) => {
  let { knowledge_id } = req.body
  const sql = `delete from knowledge where knowledge_id='${knowledge_id}'`
  db.query(sql, (err, data) => {
    if (err) return console.log('小知识删除接口错误', err.message)
    res.send('删除成功')
    console.log('小知识删除接口访问成功', date_fns())
    console.log('-------------------------------------')
  })
})
module.exports = {
  postRouter,
}
